#include "data_structure_manager.h"

Facility* facilities[MAX_FACILITIES] = { NULL };

void Manager_Change_facility_code(FacilityManager i){

}

void Menu(System* sys){
    printf("*******************************************\n");
	printf("*           欢迎使用管理员系统           *\n");
	printf("*******************************************\n");
	printf("*           请选择您的使用功能            *\n");
	printf("*                b,信息管理                  *\n");
    printf("*                c, 信息查询             *\n");
    printf("*                d, 信息排序             *\n");
    printf("*                e,信息统计            *\n");
    printf("*                f, 系统维护             *\n");
	printf("*                a,退出                   *\n");
	printf("*******************************************\n");
    char a;
    scanf_s("%c",&a);
    switch (a)
    {
    case 'b':
        Data_Manage(sys);
        break;
    case 'c':
        //data_check(sys);
        break;
    case 'd':
        //data_sort();
        break;
    case 'e':
        //data_count();
        break;
    case 'f':
        //System_defend();
        break;
    case 'a':
        break;
    default:
        printf("输入错误请重试\n");
        Data_Manage(sys);
        break;
    }
    clear_input_buffer3();
}

void Data_Manage(System* sys){
    printf("*******************************************\n");
	printf("*           欢迎使用管理员系统           *\n");
	printf("*******************************************\n");
	printf("*           请选择您的使用功能            *\n");
	printf("*                a,场地负责人信息管理           *\n");
	printf("*                b, 场地信息管理             *\n");
	printf("*                c,返回                       *\n");
	printf("*******************************************\n");

    char i;
    scanf_s("%c",&i);
    switch(i){
        case 'a':
        FacilityManager_datamanage(sys);
        break;

        case 'b':
        Facility_datamanage(sys);
        break;

        default:
            printf("输入错误请重试\n");
            Data_Manage(sys);
            break;
        case 'c':
            Menu(sys);
            break;
    }
    clear_input_buffer3();
}

void FacilityManager_datamanage(System* sys){
    printf("*******************************************\n");
	printf("*           欢迎使用管理员系统           *\n");
	printf("*******************************************\n");
	printf("*           请选择您的使用功能            *\n");
	printf("*                a,增加           *\n");
	printf("*                b,删除             *\n");
	printf("*                c,修改                  *\n");
    printf("*                d,返回                 *\n");
	printf("*******************************************\n"); 

    char i;
    scanf_s("%c",&i);
    switch(i){
    case 'a': 
        {
            int id; char name[10]; int num_facilities; char username[10]; char password[20]; long long phone;
            printf("请输入id\n"); scanf_s("%d", &id); clear_input_buffer3();
            printf("请输入姓名\n"); gets_s(name, MAX_NAME_LENGTH); clear_input_buffer3();
            printf("请输入场地数目\n"); scanf_s("%d", &num_facilities); clear_input_buffer3();
            printf("请输入用户名"); gets_s(username, MAX_USERNAME); clear_input_buffer3();
            printf("请输入密码\n"); gets_s(name, MAX_NAME_LENGTH); clear_input_buffer3();
            printf("请输入手机号\n"); scanf_s("%lld", &phone); clear_input_buffer3();
            FacilityManager new_facility_manager = create_facility_manager(id, name, num_facilities, facilities, username, password, phone);
            add_facility_manager(&sys->facility_manager_link, &new_facility_manager);
            write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
            FacilityManager_datamanage(sys);
        }
            break;
    case 'b': 
        {
            int i;
            printf("请输入删除人id");
            scanf_s("%d", &i);
            clear_input_buffer2();
            delete_facility_manager(&sys->facility_manager_link, i);
            FacilityManager_datamanage(sys);
        }
            
            break;
        case 'c':
            ////str i;
            ////scanf_s("%s",&i);
            //// 错误
            //Change_facility_manager(i);
            //FacilityManager_datamanage(sys);
        {
            int id;
            printf("请输入更改人id");
            scanf_s("%d", &i);
            FacilityManager* fm = find_facility_manager(&sys->facility_manager_link, id);
            Change_facility_manager(*fm, sys);
            FacilityManager_datamanage(sys);
        }
            
            break;
        default:
            printf("输入错误请重试\n");
            FacilityManager_datamanage(sys);
            break;
        case 'd':
            Data_Manage(sys);
            break;
    }
    print_facility_managers(&sys->facility_manager_link);
    clear_input_buffer3();

}
void Change_facility_manager(FacilityManager i,System* sys){
    print_facility_managers(&sys->facility_manager_link);
    printf("请选择更改场地管理员id");
    int id;scanf_s("%d",&id);clear_input_buffer3();
    FacilityManager *fm = find_facility_manager(&sys->facility_manager_link, id);
    printf("请选择修改项目");
    Change_fm_datailed(fm,sys);
}

void Change_fm_datailed(FacilityManager *fm,System* sys){
    char i;
    printf("*******************************************\n");
	printf("*           欢迎使用管理员系统           *\n");
	printf("*******************************************\n");
	printf("*           请选择您的使用功能            *\n");
	printf("*                a, 场地负责人ID          *\n");
	printf("*                b, 场地负责人姓名             *\n");
	printf("*                c, 场地负责人联系方式        *\n");
    printf("*                d, 场地负责人密码        *\n");
    printf("*                e, 负责人管理的场地数量                 *\n");
    printf("*                f, 返回                       *\n");
	printf("*******************************************\n"); 
    scanf_s("%c",&i);
    switch(i){
    case 'a': 
        {
            int nid;
            printf("请输入新id");
            scanf_s("%d", &nid); clear_input_buffer3();
            FacilityManager changed_facility_manager = create_facility_manager(nid, fm->name, fm->num_facilities, fm->facilities, fm->username, fm->password, fm->phone);
            add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
            delete_facility_manager(&sys->facility_manager_link, fm->id);
            Change_fm_datailed(fm, sys);
        }
            break;

        case 'b':
        {
            char nname[MAX_NAME_LENGTH];
            printf("请输入新姓名");
            gets_s(nname, MAX_NAME_LENGTH); clear_input_buffer3();
            FacilityManager changed_facility_manager = create_facility_manager(fm->id, nname, fm->num_facilities, fm->facilities, fm->username, fm->password, fm->phone);
            add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
            delete_facility_manager(&sys->facility_manager_link, fm->id);
            Change_fm_datailed(fm, sys);
        }
            break;
        case 'c':
        {
            long long nphone;
            printf("请输入新手机号");
            scanf_s("%lld", &nphone); clear_input_buffer3();
            FacilityManager changed_facility_manager = create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities, fm->username, fm->password, nphone);
            add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
            delete_facility_manager(&sys->facility_manager_link, fm->id);
            Change_fm_datailed(fm, sys);
        }
            break;

        case 'd':
            printf("请输入新密码");
            char ncode[MAX_PASSWORD];
            gets_s(ncode,MAX_PASSWORD); clear_input_buffer3();
            FacilityManager changed_facility_manager=create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities,fm->username, ncode,fm->phone);
            add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
            delete_facility_manager(&sys->facility_manager_link,fm->id);
            Change_fm_datailed(fm,sys);
            break;

        case 'e':
            printf("请输入新设备数目");
            int nnum_facility;
            scanf_s("%d",&nnum_facility);clear_input_buffer3();
            Change_facility(fm,sys);
            Change_fm_datailed(fm,sys);
            break;
        case 'f':
            FacilityManager_datamanage(sys);
            break;
        default:
            printf("输入错误");
            Change_fm_datailed(fm,sys);
            break;

    }
 }

// 修改负责人管理的具体场地信息
void Change_facility(FacilityManager* fm,System* sys) {
    if (fm == NULL || fm->num_facilities == 0) {
        printf("该负责人暂无管理场地\n");
        return;
    }

    // 显示当前管理的所有场地
    printf("\n当前管理的场地列表\n");
    for (int i = 0; i < fm->num_facilities; i++) {
        Facility* f = fm->facilities[i];
        printf("[%d] ID:%d 名称:%s 类型:", i+1, f->id, f->name);
        print_facility_type(f->type);
        printf("\n");
    }

    // 选择要修改的场地
    int index;
    printf("\n请选择要修改的场地序号(1-%d): ", fm->num_facilities);
    scanf_s("%d", &index);
    clear_input_buffer2();
    
    if (index < 1 || index > fm->num_facilities) {
        printf("输入序号无效\n");
        return;
    }
    
    Facility* target = fm->facilities[index-1];

    // 显示修改菜单
    printf("\n请选择要修改的项目：");
    printf("\n1. 场地名称");
    printf("\n2. 场地类型");
    printf("\n3. 场地面积");
    printf("\n4. 开放时间");
    printf("\n5. 价格/半小时");
    printf("\n6. 最大容纳人数");
    printf("\n0. 返回上级");
    printf("\n请输入选项：");
    
    char choice;
    scanf_s("%c", &choice);
    clear_input_buffer2();

    switch(choice) {
        case '1': {  // 修改名称
            char new_name[MAX_NAME_LENGTH];
            printf("请输入新名称（最多%d字符）: ", MAX_NAME_LENGTH-1);
            fgets(new_name, MAX_NAME_LENGTH, stdin);
            new_name[strcspn(new_name, "\n")] = '\0';  // 去除换行符
            strncpy_s(target->name, MAX_NAME_LENGTH, new_name, _TRUNCATE);
            break;
        }
        
        case '2': {  // 修改类型
            printf("\n可选类型：");
            printf("\n1-篮球场 2-羽毛球场 3-足球场");
            printf("\n4-台球场 5-乒乓球场 6-排球场");
            printf("\n请输入新类型编号：");
            
            int type;
            scanf_s("%d", &type);
            clear_input_buffer2();
            
            if (type < 1 || type > 6) {
                printf("无效类型编号\n");
                return;
            }
            target->type = (VenueType)type;
            break;
        }

        case '3': {  // 修改面积
            printf("请输入新面积（平方米）: ");
            int area;
            scanf_s("%d", &area);
            clear_input_buffer2();
            target->area = area > 0 ? area : target->area;
            break;
        }

        case '4': {  // 修改开放时间
            char new_time[MAX_NAME_LENGTH];
            printf("请输入新开放时间（格式示例：08:00-22:00）: ");
            fgets(new_time, MAX_NAME_LENGTH, stdin);
            new_time[strcspn(new_time, "\n")] = '\0';
            strncpy_s(target->open_time, MAX_NAME_LENGTH, new_time, _TRUNCATE);
            break;
        }

        case '5': {  // 修改价格
            printf("请输入新价格（元/半小时）: ");
            float price;
            scanf_s("%f", &price);
            clear_input_buffer2();
            target->price_per_half_hour = price > 0 ? price : target->price_per_half_hour;
            break;
        }

        case '6': {  // 修改最大人数
            printf("请输入新最大容纳人数: ");
            int max_num;
            scanf_s("%d", &max_num);
            clear_input_buffer2();
            target->max_person_num = max_num > 0 ? max_num : target->max_person_num;
            break;
        }

        case '0':
            return;

        default:
            printf("无效选项\n");
    }

    // 写入文件保存修改
    write_facility_file(*sys, "facility.txt");      
    FacilityManager changed_facility_manager=create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities,fm->username, fm->password,fm->phone);
    add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
    delete_facility_manager(&sys->facility_manager_link,fm->id);

    printf("\n修改成功\n");
}

void Facility_datamanage(System* sys){
    printf("*******************************************\n");
	printf("*           欢迎使用管理员系统           *\n");
	printf("*******************************************\n");
	printf("*           请选择您的使用功能            *\n");
	printf("*                a,增加           *\n");
	printf("*                b, 删除             *\n");
	printf("*                c,修改                  *\n");
    printf("*                d,返回                 *\n");
	printf("*******************************************\n"); 

    char i;
    scanf_s("%c",&i);
    switch(i){
        case 'a':
            int id;char* name;int manager_id;int type;int area;char* open_time;float price;int max_person_num;
            printf("请输入场地编号\n");scanf_s("%d",&id);clear_input_buffer3();
            printf("请输入负责人id\n");scanf_s("%d",&manager_id);clear_input_buffer3();
            printf("请输入负责人姓名\n");gets_s(name,MAX_NAME_LENGTH);clear_input_buffer3();
            printf("请输入场地类型\n");scanf_s("%d",&type);clear_input_buffer3();
            printf("请输入场地面积");scanf_s("%d",&area);clear_input_buffer3();
            printf("请输入开放时间\n");gets_s(open_time, MAX_NAME_LENGTH);clear_input_buffer3();
            printf("请输入价格\n");scanf_s("%f",&price);clear_input_buffer3();
            printf("请输入最大容纳人数\n");scanf_s("%d",&max_person_num);
            Facility f=create_facility(*sys,id,name,manager_id,type,area,open_time,price,max_person_num);
            add_facility(&sys->facility_link,&f);
            write_facility_file(*sys, "..\\src\\facility.txt");
            Facility_datamanage(sys);
            break;
        case 'b':
            int i;
            printf("请输入删除场地id");
            scanf_s("%d",&i);clear_input_buffer3();
            delete_facility(&sys->facility_link,i);
            break;

        case 'c':
            printf("请输入更改场地id");
            scanf_s("%d",&i);clear_input_buffer3();

            break;
        default:
            printf("输入错误");
            Facility_datamanage(sys);
            break;
        case 'd':// 缺少id
            int facility_id;
            printf("请输入更改人id");
            scanf_s("%d", &id);
            Facility *f=find_facility(&sys->facility_link,facility_id);
            printf("请选择修改项目");
            Change_f_datailed(&f,sys);
            break;
    }
    print_facility_managers(&sys->facility_manager_link);
}

void Change_f_datailed(Facility *f,System* sys) {
    printf("\n请选择要修改的项目：");
    printf("\na. 场地名称");
    printf("\nb. 场地类型");
    printf("\nc. 场地面积");
    printf("\nd. 开放时间");
    printf("\ne. 价格/半小时");
    printf("\nf. 最大容纳人数");
    printf("\ng. 返回");
    printf("\n请输入选项：");
    
    char c;
    scanf_s("%c", &c);
    clear_input_buffer3();

    switch (c) {
        case 'a': {  // 修改名称
            char new_name[MAX_NAME_LENGTH];
            printf("请输入新名称（最多%d字符）: ", MAX_NAME_LENGTH-1);
            fgets(new_name, MAX_NAME_LENGTH, stdin);
            new_name[strcspn(new_name, "\n")] = '\0';  // 去除换行符
            strncpy_s(f->name, MAX_NAME_LENGTH, new_name, _TRUNCATE);
            break;
        }
        
        case 'b': {  // 修改类型
            printf("\n可选类型：");
            printf("\n1-篮球场 2-羽毛球场 3-足球场");
            printf("\n4-台球场 5-乒乓球场 6-排球场");
            printf("\n请输入新类型编号：");
            
            int type;
            scanf_s("%d", &type);
            clear_input_buffer3();
            
            if (type < 1 || type > 6) {
                printf("无效类型编号，请保持原值\n");
                return;
            }
            f->type = (VenueType)type;
            break;
        }

        case 'c': {  // 修改面积
            printf("请输入新面积（平方米）: ");
            int area;
            scanf_s("%d", &area);
            clear_input_buffer3();
            
            if (area <= 0) {
                printf("面积必须大于0，请保持原值\n");
                return;
            }
            f->area = area;
            break;
        }

        case 'd': {  // 修改开放时间
            char new_time[MAX_NAME_LENGTH];
            printf("请输入新开放时间（格式示例：08:00-22:00）: ");
            fgets(new_time, MAX_NAME_LENGTH, stdin);
            new_time[strcspn(new_time, "\n")] = '\0';
            strncpy_s(f->open_time, MAX_NAME_LENGTH, new_time, _TRUNCATE);
            break;
        }

        case 'e': {  // 修改价格
            printf("请输入新价格（元/半小时）: ");
            float price;
            scanf_s("%f", &price);
            clear_input_buffer3();
            
            if (price <= 0) {
                printf("价格必须大于0，请保持原值\n");
                return;
            }
            f->price_per_half_hour = price;
            break;
        }

        case 'f': {  // 修改最大人数
            printf("请输入新最大容纳人数: ");
            int max_num;
            scanf_s("%d", &max_num);
            clear_input_buffer3();
            
            if (max_num <= 0) {
                printf("人数必须大于0，请保持原值\n");
                return;
            }
            f->max_person_num = max_num;
            break;
        }

        case 'g': {  // 返回
            return;
        }

        default: {
            printf("无效选项，请重新输入\n");
            Change_f_datailed(f,sys);  // 递归调用以重新选择
            return;
        }
    }

    // ====== 保存修改到文件 ====== //
    write_facility_file(*sys, "..\\src\\facility.txt");
    printf("\n修改成功！\n");
}






//
////第二部分分界线
//void data_check(System* sys) {
//    printf("\n请选择查询类型：");
//    printf("\n1. 用户预订记录");
//    printf("\n2. 场地空闲状态");
//    printf("\n3. 用户活动记录");
//    printf("\n4. 模糊搜索场地");
//    printf("\n0. 返回上级");
//        
//    char choice;
//    scanf_s("%c", &choice);
//    clear_input_buffer3();
//        
//    switch (choice) {
//        case '1': {
//            char phone[MAX_PHONE_LENGTH];
//            printf("请输入用户手机号：");
//            scanf_s_s("%s", phone, MAX_PHONE_LENGTH);clear_input_buffer3();
//            User* user = find_user_by_phone(&sys->user_link, phone);
//            query_user_reservations(user,sys);
//            break;
//        }
//            
//        case '2': {
//            int facility_id;
//            char date[11], start[6], end[6];
//            printf("请输入场地ID：");
//            scanf_s("%d", &facility_id);clear_input_buffer3();
//            printf("请输入日期（YYYY-MM-DD）：");
//            scanf_s_s("%s", date, 11);clear_input_buffer3();
//            printf("请输入开始时间（HH:MM）：");
//            scanf_s_s("%s", start, 6);clear_input_buffer3();
//            printf("请输入结束时间（HH:MM）：");
//            scanf_s_s("%s", end, 6);clear_input_buffer3();
//            Facility* f = find_facility(&sys->facility_link, facility_id);
//            query_facility_availability(f, date, start, end);
//            break;
//        }
//            
//        case '3': {
//            char phone[MAX_PHONE_LENGTH], start_date[11], end_date[11];
//            printf("请输入用户手机号：");
//            scanf_s_s("%s", phone, MAX_PHONE_LENGTH);clear_input_buffer3();
//            printf("请输入开始日期（YYYY-MM-DD）：");
//            scanf_s_s("%s", start_date, 11);clear_input_buffer3();
//            printf("请输入结束日期（YYYY-MM-DD）：");
//            scanf_s_s("%s", end_date, 11);clear_input_buffer3();
//            User* user = find_user_by_phone(sys->user_link, phone);
//            query_user_activity(user, start_date, end_date);
//            break;
//        }
//            
//        case '4': {
//            char keyword[MAX_NAME_LENGTH];
//            printf("请输入搜索关键词：");
//            fgets(keyword, MAX_NAME_LENGTH, stdin);
//            keyword[strcspn(keyword, "\n")] = '\0';
//            fuzzy_search_facility(keyword);
//            break;
//        }
//            
//        case '0':
//            return;
//                
//        default:
//            printf("无效选项");
//    }
//}
//
//#include "data_structure.h"
//#include <string.h>
//#include <ctype.h>
//
//// 模糊字符串匹配（不区分大小写）
//int fuzzy_match(const char* str, const char* pattern) {
//    if (str == NULL || pattern == NULL) return 0;
//    while (*str) {
//        const char* s = str;
//        const char* p = pattern;
//        while (*s && *p && tolower(*s) == tolower(*p)) {
//            s++;
//            p++;
//        }
//        if (*p == '\0') return 1;
//        str++;
//    }
//    return 0;
//}
//
//// 检查时间段是否重叠
//int is_time_overlap(char* start1, char* end1, char* start2, char* end2) {
//    int s1 = time_to_minutes(start1);
//    int e1 = time_to_minutes(end1);
//    int s2 = time_to_minutes(start2);
//    int e2 = time_to_minutes(end2);
//    return (s1 < e2) && (s2 < e1);
//}
//
//// 时间字符串转分钟数（如 "08:30" -> 510）
//int time_to_minutes(const char* time_str) {
//    int hour, min;
//    sscanf_s_s(time_str, "%d:%d", &hour, &min);
//    return hour * 60 + min;
//}
//
//// 查询用户所有预订记录
////void query_user_reservations(User* user,System* sys) {
////    if (user == NULL) return;
////    
////    printf("\n用户 [%s] 的预订记录：\n", user->id_nicknames);
////    Res_node* node = sys->reservation_link.head->next;
////    int count = 0;
////    
////    while (node != NULL) {
////        if (node->user_res.user->id == user->id) {
////            printf("预订ID: %d\n", node->reservation.id);
////            printf("场地: %s (ID:%d)\n", 
////                   node->reservation.facility->name, 
////                   node->reservation.facility->id);
////            printf("时间: %s-%s\n", 
////                   node->reservation.begin_time, 
////                   node->reservation.end_time);
////            printf("状态: %s\n", node->reservation.is_cancelled ? "已取消" : "有效");
////            printf("------------------------\n");
////            count++;
////        }
////        node = node->next;
////    }
////    
////    if (count == 0) {
////        printf("未找到相关预订记录\n");
////    }
////}
//
////// 查询场地时间段空闲状态
////void query_facility_availability(System* sys,Facility* facility, char* date, char* start_time, char* end_time) {
////    if (facility == NULL) return;
////    
////    printf("\n场地 [%s] 在 %s %s-%s 的预订情况：\n", 
////           facility->name, date, start_time, end_time);
////    Res_node* node = sys->reservation_link.head->next;
////    int is_available = 1;
////    
////    while (node != NULL) {
////        if (node->reservation.facility->id == facility->id &&
////            strcmp(node->reservation.date, date) == 0 &&
////            is_time_overlap(node->reservation.begin_time, 
////                            node->reservation.end_time, 
////                            start_time, end_time)) {
////            printf("冲突预订：用户 %s (ID:%d) 已预订\n", 
////                   node->reservation.user->username, 
////                   node->reservation.user->id);
////            is_available = 0;
////        }
////        node = node->next;
////    }
////    
////    if (is_available) {
////        printf("该时间段空闲\n");
////    }
////}
//
//// 组合查询：用户+日期范围
////void query_user_activity(System* sys,User* user, char* start_date, char* end_date) {
////    if (user == NULL) return;
////    
////    printf("\n用户 [%s] 在 %s 至 %s 的活动记录：\n", 
////           user->id_nicknames, start_date, end_date);
////    RENode* node = sys->record_link.head->next;
////    int count = 0;
////    
////    while (node != NULL) {
////        if (node->record.user->id == user->id &&
////            strcmp(node->record.date, start_date) >= 0 &&
////            strcmp(node->record.date, end_date) <= 0) {
////            printf("日期: %s\n", node->record.date);
////            printf("场地: %s\n", node->record.facility->name);
////            printf("时间: %s-%s\n", node->record.begin_time, node->record.end_time);
////            printf("状态: %s\n", node->record.is_end ? "已结束" : "进行中");
////            printf("------------------------\n");
////            count++;
////        }
////        node = node->next;
////    }
////    
////    if (count == 0) {
////        printf("未找到相关活动记录\n");
////    }
////}
//
//// 模糊查询场地
//void fuzzy_search_facility(const char* keyword,System* sys) {
//    printf("\n匹配 \"%s\" 的场地：\n", keyword);
//    FNode* node = sys->facility_link.head->next;
//    int found = 0;
//    
//    while (node != NULL) {
//        if (fuzzy_match(node->facility.name, keyword) ||
//            fuzzy_match(node->facility.open_time, keyword)) {
//            printf("ID: %d\n", node->facility.id);
//            printf("名称: %s\n", node->facility.name);
//            printf("类型: ");
//            print_facility_type(node->facility.type);
//            printf("\n开放时间: %s\n", node->facility.open_time);
//            printf("------------------------\n");
//            found++;
//        }
//        node = node->next;
//    }
//    
//    if (!found) {
//        printf("未找到匹配场地\n");
//    }
//}
//
//
//// 场地属性比较函数类型
//typedef int (*FacilityCompareFunc)(const Facility*, const Facility*);
//
//// 按面积升序
//int compare_area_asc(const Facility* a, const Facility* b) {
//    return a->area - b->area;
//}
//
//// 按价格降序
//int compare_price_desc(const Facility* a, const Facility* b) {
//    return (b->price_per_half_hour > a->price_per_half_hour) ? 1 : -1;
//}
//
//// 多属性排序：先类型后面积
//int compare_type_area(const Facility* a, const Facility* b) {
//    if (a->type != b->type) {
//        return a->type - b->type;  // 类型优先
//    }
//    return a->area - b->area;      // 类型相同则按面积
//}
//
//// 链表节点交换（用于冒泡排序）
//void swap_nodes(FNode* prev, FNode* a, FNode* b) {
//    prev->next = b;
//    a->next = b->next;
//    b->next = a;
//}
//
//// 通用场地链表排序
//void sort_facility_list(FList* list, FacilityCompareFunc compare, int ascending) {
//    if (list->num < 2) return;
//    
//    int swapped;
//    FNode dummy;
//    dummy.next = list->head->next;
//    
//    do {
//        swapped = 0;
//        FNode* prev = &dummy;
//        FNode* curr = dummy.next;
//        
//        while (curr->next != NULL) {
//            FNode* next = curr->next;
//            int result = compare(&curr->facility, &next->facility);
//            
//            // 处理升序/降序
//            if ((ascending && result > 0) || (!ascending && result < 0)) {
//                swap_nodes(prev, curr, next);
//                swapped = 1;
//                prev = next;  // 交换后prev指向新节点
//            } else {
//                prev = curr;
//                curr = next;
//            }
//        }
//    } while (swapped);
//    
//    list->head->next = dummy.next;
//}
//
//// 对查询结果排序（示例：场地列表）
//void sort_query_results(FList* results) {
//    printf("\n选择排序方式：");
//    printf("\n1. 按面积升序");
//    printf("\n2. 按价格降序");
//    printf("\n3. 类型+面积组合排序");
//    printf("\n请输入选项：");
//    
//    char choice;
//    scanf_s("%c", &choice);
//    clear_input_buffer3();
//    
//    FacilityCompareFunc compare = NULL;
//    int ascending = 1;
//    
//    switch(choice) {
//        case '1':
//            compare = compare_area_asc;
//            break;
//        case '2':
//            compare = compare_price_desc;
//            ascending = 0;
//            break;
//        case '3':
//            compare = compare_type_area;
//            break;
//        default:
//            printf("无效选项\n");
//            return;
//    }
//    
//    sort_facility_list(results, compare, ascending);
//    printf("\n排序完成\n");
//}
//
//// 预订记录按时间排序
//int compare_reservation_time(const Reservation* a, const Reservation* b) {
//    int date_cmp = strcmp(a->date, b->date);
//    if (date_cmp != 0) return date_cmp;
//    return time_to_minutes(a->begin_time) - time_to_minutes(b->begin_time);
//}
//
//// 使用记录按持续时间排序
//int compare_usage_duration(const Record* a, const Record* b) {
//    int a_dur = time_to_minutes(a->end_time) - time_to_minutes(a->begin_time);
//    int b_dur = time_to_minutes(b->end_time) - time_to_minutes(b->begin_time);
//    return a_dur - b_dur;
//}
